﻿/*
 * Test classes for SSAS Partition Manager 
 * https://ssaspartitionmanager.codeplex.com/
 * 
 * Written by Dr. John Tunnicliffe of Decision Analytics, May 2015
 * eMail: john@decision-analytics.co.uk 
 * http://www.decision-analytics.co.uk/
 * https://www.linkedin.com/in/drjohntunnicliffe
 * 
 * Tests written with FluentAssertions, XUnit and Moq which can be installed from Nuget.  
 * 
 * FluentAssertions
 * http://www.fluentassertions.com/ 
 * 
 * XUnit
 * http://xunit.github.io/
 * 
 * Moq
 * https://github.com/Moq/moq4
 */

namespace AmoExtensions.IntegrationTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.SqlClient;
    using System.Dynamic;
    using Dapper;

    /// <summary>
    /// Common methods to create mocked objects
    /// </summary>
    internal static class FixtureHelperFunctions
    {
        /// <summary>
        /// Connects the AmoServer class to the SSAS multidimensional dev server defined in the App.Config file
        /// </summary>
        /// <param name="server"></param>
        /// <returns></returns>
        internal static bool ConnectSsasServer(IAmoServer server)
        {
            return server.Connect(AmoExtensions.IntegrationTests.Properties.Settings.Default.SsasServerName);
        }

        /// <summary>
        /// Connects the AmoServer class to the SSAS Tabular dev server defined in the App.Config file
        /// </summary>
        /// <param name="server"></param>
        /// <returns></returns>
        internal static bool ConnectTabularServer(IAmoServer server)
        {
            return server.Connect(AmoExtensions.IntegrationTests.Properties.Settings.Default.tabularServerName);
        }

        /// <summary>
        /// Generates a unique name for the new SSAS database
        /// </summary>
        /// <returns></returns>
        internal static string GetUniqueSsasDatabaseName()
        {
            // create a crazy database name for use in our tests
            // we are pretty much garenteed that this will not already exist!
            return "Test_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// Deploys a copy of the AdventureWorks cube using the name and server provided.
        /// The full path to the AdventureWorks definition should be supplied in the App.Config file
        /// along with the ConnectionString for the cube to the source Data Mart
        /// </summary>
        /// <param name="server">Connection to the SSAS server</param>
        /// <param name="SsasDatabaseName">Name of the new datatbase</param>
        /// <returns></returns>
        internal static bool DeployTestCube(IAmoServer server, string SsasDatabaseName)
        {
            return server.DeployDatabase(
                    SsasDatabaseName,
                    AmoExtensions.IntegrationTests.Properties.Settings.Default.FullPathToOlapDatabaseDefinition,
                    AmoExtensions.IntegrationTests.Properties.Settings.Default.ConnectionStringForCubeToSourceDataMart);
        }


        /// <summary>
        /// Deploys a copy of the AdventureWorks tabular model using the name and server provided.
        /// The full path to the AdventureWorks tabular model definition should be supplied in the App.Config file
        /// </summary>
        /// <param name="server">Connection to the SSAS server</param>
        /// <param name="SsasDatabaseName">Name of the new datatbase</param>
        /// <returns></returns>
        internal static bool DeployTestModel(IAmoServer server, string tabularDatabaseName)
        {
            return server.DeployDatabase(
                    tabularDatabaseName,
                    AmoExtensions.IntegrationTests.Properties.Settings.Default.FullPathToTabularDatabaseDefinition);
        }

        /// <summary>
        /// Helper method to execute a query and return the results using Dapper library
        /// </summary>
        /// <param name="sql">The SQL query to execute.</param>
        /// <returns>
        /// A sequence of dynamic objects representing the result set.
        /// </returns>
        internal static IEnumerable<dynamic> QueryDynamic(string sql)
        {
            using (var connection = new SqlConnection(AmoExtensions.IntegrationTests.Properties.Settings.Default.configDatabaseConnectionString))
            {
                connection.Open();
                return connection.Query(sql);
            }
        }
    }
}
